<?xml version="1.0" encoding="UTF-8"?>

<!--

    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<html>
<head>
<meta charset="utf-8"/>
<title>CXF JAX-RS WebSocket Sample</title>  
</head>
<body>
<h2>CXF JAX-RS WebSocket Sample</h2>  

<div id="output">
  <div id="config" style="float: left;">
    <strong>Service Endpoint URL:</strong><br />
    <input id="wsUri" size="72" style="width: 100%" value="ws://localhost:9000/demo"/>
    <br/>
    <button id="connect">Connect</button>
    <button id="disconnect">Disconnect</button>
    <br />
    <br />
    <strong>Request:</strong><br />
    <textarea id="request" rows="10" cols="72" style="width: 100%;">GET /demo/customerservice/customers/123</textarea>
    <br/>
    <button id="send">Send</button>
  </div>
  <div id="log" style="float: left; margin-left: 20px; padding-left: 20px; width: 60%; height: 500px; border-left: solid 1px #cccccc;">
    <strong>Log:</strong><br />
    <button id="clearLogBut" style="position: relative; top: 3px;">Clear log</button>
    <div id="consoleLog" style="margin-top: 5px; border: solid 1px #aaaaaa; overflow-y: scroll; height:100%"></div>
  </div>
  <div class="clearfix"></div>
</div>
<script language="javascript" type="text/javascript">
  var output;

  var connectBut = document.getElementById("connect");
  connectBut.onclick = doConnect;

  var disconnectBut = document.getElementById("disconnect");
  disconnectBut.onclick = doDisconnect;
    
  var request = document.getElementById("request");

  var sendBut = document.getElementById("send");
  sendBut.onclick = doSend;

  var consoleLog = document.getElementById("consoleLog");

  var clearLogBut = document.getElementById("clearLogBut");
  clearLogBut.onclick = clearLog;

  function init() {
    output = document.getElementById("output");
    setGuiConnected(false);
  }

  function doConnect() {
    var wsUri = document.getElementById("wsUri");

    websocket = new WebSocket(wsUri.value);
    websocket.binaryType = 'arraybuffer';

    websocket.onopen = function(evt) {
      onOpen(evt)
    };

    websocket.onclose = function(evt) {
       onClose(evt)
    };

    websocket.onmessage = function(evt) {
      onMessage(evt)
    };

    websocket.onerror = function(evt) {
      onError(evt)
    };
  }

  function doDisconnect() {
    websocket.close()
  }

  function clearLog() {
    while (consoleLog.childNodes.length > 0) {
      consoleLog.removeChild(consoleLog.lastChild);
    }
  }

  function onOpen(evt) {
    writeToLog('<span style="color:black; font-size:75%; font-style:italic">CONNECTED</span>');
    setGuiConnected(true);
  }

  function onClose(evt) {
    writeToLog('<span style="color:black; font-size:75%; font-style:italic">DISCONNECTED</span>');
    setGuiConnected(false);
  }

  function onMessage(evt) {
    writeToLog('<span style="color:blue; font-size:75%; font-style:italic">RECEIVED</span><span style="color: blue"><pre>' + createMessage(evt.data) + '</pre></span>');
  }

  function onError(evt) {
    writeToLog('<span style="color:red; font-size:75%; font-style:italic">ERROR</span><span style="color: red;"><pre>' + evt.data + '</pre></span>');
  }

  function doSend(evt) {
    var reqmsg = document.getElementById("request");
    writeToLog('<span style="color:green; font-size:75%; font-style:italic">SENT</span><span style="color: green;"><pre>' + createMessage(reqmsg.value) + '</pre></span>');
    websocket.send(reqmsg.value);
  }

  function setGuiConnected(isConnected) {
    wsUri.disabled = isConnected;
    connectBut.disabled = isConnected;
    disconnectBut.disabled = !isConnected;
    request.disabled = !isConnected;
    sendBut.disabled = !isConnected;
  }

  function createMessage(data) {
    if (typeof data != 'string') {
      data = arrayBufferToString(data);
    }
    return data.replace(/&/g, '&amp;').replace(/</g, '&lt;');
  }

  function arrayBufferToString(buf) {
    var ba = new Uint8Array(buf);
    var aa = new Array(ba.length);
    for (i = 0; i < ba.length; i++) {
      aa[i] = String.fromCharCode(ba[i]);
    }
    return Array.prototype.slice.call(aa).join("");
  }

  function writeToLog(message) {
    var pre = document.createElement("p");
    pre.wordWrap = "break-word";
    pre.innerHTML = message;
    consoleLog.appendChild(pre);

    while (consoleLog.childNodes.length > 50) {
      consoleLog.removeChild(consoleLog.firstChild);
    }
    
    consoleLog.scrollTop = consoleLog.scrollHeight;
  }

  window.addEventListener("load", init, false);

  </script>  
</body>
</html>
